﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.IO;
using System.Text.RegularExpressions;
using BlammoNet.Enum;
using BlammoNet.Interfaces;
#endregion

namespace BlammoNet.BaseClasses
{
    /// <summary>
    /// Base class for logs
    /// </summary>
    public class Log : ILog, IDisposable
    {
        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Name">Log name</param>
        public Log(string Name)
        {
            this.FileName = Path.Combine(Manager.Configuration.LogFileLocation, Name);
            CurrentFileNumber = 1;
            CurrentDate = DateTime.Now;
            for (int x = 1; x <= Manager.Configuration.NumberOfFiles; ++x)
            {
                Start(x);
            }
            CurrentFileName = GetFileName(CurrentFileNumber, Manager.Configuration.FileFormat);
        }

        #endregion

        #region Protected Functions

        /// <summary>
        /// Adds header information to the 
        /// </summary>
        /// <param name="FileNumberUsing"></param>
        protected virtual void Start(int FileNumberUsing)
        {

            string Message = Manager.Configuration.Header;
            string TempFileFormat = Manager.Configuration.FileFormat;
            TempFileFormat = GetFileName(FileNumberUsing, TempFileFormat);
            DisplayHeader(Message, TempFileFormat);
        }

        /// <summary>
        /// Actually displays the header information (must be overridden)
        /// </summary>
        /// <param name="Message">Header message</param>
        /// <param name="TempFileFormat">Name of the file to use (may not be needed)</param>
        protected virtual void DisplayHeader(string Message, string TempFileFormat)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the file name in use
        /// </summary>
        /// <param name="FileNumberUsing">Current File Number</param>
        /// <param name="TempFileFormat">File format in use</param>
        /// <returns>The formatted file name to use</returns>
        protected virtual string GetFileName(int FileNumberUsing, string TempFileFormat)
        {
            TempFileFormat = Current.Replace(TempFileFormat, CurrentDate.ToString(Manager.Configuration.FileDateTimeFormat));
            TempFileFormat = Name.Replace(TempFileFormat, FileName);
            TempFileFormat = FileNumber.Replace(TempFileFormat, FileNumberUsing.ToString());
            return TempFileFormat;
        }

        /// <summary>
        /// Gets the formatted message
        /// </summary>
        /// <param name="MessageFormat">Message format to use</param>
        /// <param name="TempMessageType">Message type</param>
        /// <param name="MessageString">Actual message string</param>
        /// <returns>The formatted message</returns>
        protected virtual string GetMessage(string MessageFormat, MessageType TempMessageType, string MessageString)
        {
            MessageFormat = Current.Replace(MessageFormat, DateTime.Now.ToString(Manager.Configuration.MessageDateTimeFormat));
            MessageFormat = MessageType.Replace(MessageFormat, TempMessageType.ToString());
            MessageFormat = Message.Replace(MessageFormat, MessageString);
            return MessageFormat;
        }

        /// <summary>
        /// Ends the log
        /// </summary>
        /// <param name="Message">Message to display</param>
        /// <param name="FileNumber">File number</param>
        protected virtual void End(string Message, int FileNumber)
        {
            string TempFileName = GetFileName(FileNumber, Manager.Configuration.FileFormat);
            DisplayFooter(Message, TempFileName);
        }

        /// <summary>
        /// Actually displays the footer information (must be overridden)
        /// </summary>
        /// <param name="Message">Header message</param>
        /// <param name="TempFileName">Name of the file to use (may not be needed)</param>
        protected virtual void DisplayFooter(string Message, string TempFileName)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Protected Properties

        /// <summary>
        /// File name
        /// </summary>
        protected string FileName { get; set; }

        /// <summary>
        /// Current file number
        /// </summary>
        protected int CurrentFileNumber { get; set; }

        /// <summary>
        /// The full current file name
        /// </summary>
        protected string CurrentFileName { get; set; }

        /// <summary>
        /// Gets the current date/time (used in formatting)
        /// </summary>
        protected DateTime CurrentDate { get; set; }

        #endregion

        #region Private Variables

        private Regex Current = new Regex(@"(?<Current><Current>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Regex Name = new Regex(@"(?<Name><Name>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Regex FileNumber = new Regex(@"(?<FileNumber><FileNumber>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Regex MessageType = new Regex(@"(?<MessageType><MessageType>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Regex Message = new Regex(@"(?<Message><Message>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        #endregion

        #region Interface Functions

        public virtual void Dispose()
        {
            string Message = Manager.Configuration.Footer;
            for (int x = 1; x <= Manager.Configuration.NumberOfFiles; ++x)
            {
                End(Message, x);
            }
        }

        public virtual void LogMessage(string Message, Enum.MessageType Type, params object[] args)
        {
            throw new NotImplementedException();
        }

        public virtual void TimeElapsed()
        {
            CurrentFileNumber = CurrentFileNumber % Manager.Configuration.NumberOfFiles;
            if (CurrentFileNumber == 0)
            {
                for (int x = 1; x <= Manager.Configuration.NumberOfFiles; ++x)
                {
                    End(Manager.Configuration.Footer, x);
                }
                CurrentDate = DateTime.Now;
                ++CurrentFileNumber;
                for (int x = 1; x <= Manager.Configuration.NumberOfFiles; ++x)
                {
                    Start(x);
                }
                CurrentFileName = GetFileName(CurrentFileNumber, Manager.Configuration.FileFormat);
            }
            else
            {
                ++CurrentFileNumber;
                Start(CurrentFileNumber);
                CurrentFileName = GetFileName(CurrentFileNumber, Manager.Configuration.FileFormat);
            }
        }

        #endregion
    }
}